<?php

namespace App\Admin\Controllers\Aliyun;

use App\Facades\AliyunCdn;
use App\Facades\AliyunDcdn;
use App\Facades\AliyunDomain;
use App\Models\Aliyun;
use App\Models\Application;
use Dcat\Admin\Admin;
use Dcat\Admin\Exception\InvalidArgumentException;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Exception;

class AliyunController extends AdminController
{
    protected string $title = '阿里云管理';

    /**
     * @throws InvalidArgumentException
     */
    public function grid(): Grid
    {
        $model = Aliyun::ofAdmin()
            ->withCount(['objectStorageServices'])
            ->latest();

        return Grid::make($model, function (Grid $grid) {
            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
            }
            $grid->enableDialogCreate();

            $grid->column('name', '云账户名称');
            $grid->column('app_id', 'AccessKeyID')
                ->copyable();
            $grid->column('域名')
                ->display(function () {
                    try {
                        $total = AliyunDomain::aliyun($this)->total();
                        $route = route('admin.aliyun.domains.index', $this);

                        return sprintf('<a href="%s">域名(%d)</a>', $route, $total);
                    } catch (Exception) {
                        return 'FAILED';
                    }
                });

            $grid->column('CDN')
                ->display(function () {
                    try {
                        $total = AliyunCdn::aliyun($this)->total();
                        $route = route('admin.aliyun.cdn.index', $this);

                        return sprintf('<a href="%s">CDN(%d)</a>', $route, $total);
                    } catch (Exception) {
                        return 'FAILED';
                    }
                });

            $grid->column('全站加速')
                ->display(function () {
                    try {
                        $total = AliyunDcdn::aliyun($this)->total();
                        $route = route('admin.aliyun.dcdn.index', $this);

                        return sprintf('<a href="%s">全站加速(%d)</a>', $route, $total);
                    } catch (Exception) {
                        return 'FAILED';
                    }
                });

            $grid->column('object_storage_services_count', '存储桶');
            $grid->column('created_at');
        });
    }

    /**
     * @throws InvalidArgumentException
     */
    public function form(): Form
    {
        return Form::make(Aliyun::ofAdmin(), function (Form $form) {
            if (Admin::user()->isAdministrator()) {
                $form->select('application_id', '所属应用')
                    ->required()
                    ->options(Application::ofEnabled()->pluck('name', 'id'));
            }

            $form->text('name', '云账户名称')
                ->rules('required|bail|min:2|max:32')
                ->required();
            $form->text('app_id', 'AccessKeyID')
                ->required()
                ->rules('required|bail|alpha_num|size:24')
                ->help('需要开启的权限<br/>AliyunCDNFullAccess<br/>AliyunOSSFullAccess<br/>AliyunDomainFullAccess<br/>AliyunDNSFullAccess<br/>AliyunDCDNFullAccess');
            $form->text('app_secret', 'AccessKeySecret')
                ->rules('required|bail|alpha_num|size:30')
                ->required();
        });
    }
}
